package org.zju.demo.shoppingdemo.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.zju.demo.shoppingdemo.R;
import org.zju.demo.shoppingdemo.application.ShoppingApplication;
import org.zju.demo.shoppingdemo.controller.UserInfoController;
import org.zju.demo.shoppingdemo.model.Request.LoginModel;
import org.zju.demo.shoppingdemo.model.response.BaseResponse;
import org.zju.demo.shoppingdemo.utils.Constant;

/**
 * Created by ZJDX on 2015/9/8.
 */
public class LoginActivity extends CommonActivity{

    private TextView loginTextView = null;
    private TextView registerTextView = null;
    private TextView forgetPasswordTextView = null;
    private EditText cellPhoneEditText = null;
    private EditText passwordEditText = null;
    LoginListener listener = new LoginListener(this);
    private ProgressDialog progressDialog = null;
    private LoginModel model;

    private SharedPreferences shared;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        super.textViewTitle = (TextView)findViewById(R.id.title);
        super.textViewTitle.setText(R.string.user_login);

        cellPhoneEditText = (EditText) findViewById(R.id.cell_phone);
        passwordEditText = (EditText) findViewById(R.id.pwd);
        loginTextView = (TextView) findViewById(R.id.login);
        registerTextView = (TextView) findViewById(R.id.register);
        forgetPasswordTextView = (TextView) findViewById(R.id.forget_password);

        loginTextView.setOnClickListener(listener);
        registerTextView.setOnClickListener(listener);
        forgetPasswordTextView.setOnClickListener(listener);
        initBottomMenu();
        progressDialog = new ProgressDialog(this);
    }

    @Override
    public void initBottomMenu() {
        imageViewIndex = (ImageView) findViewById(R.id.menu_home_img);
        imageViewIndex.setOnTouchListener(indexListener);
        imageViewIndex.setImageResource(R.drawable.menu_home_released);

        imageViewShopping = (ImageView) findViewById(R.id.menu_shopping_cart_img);
        imageViewShopping.setOnTouchListener(shoppingListener);
        imageViewShopping.setImageResource(R.drawable.menu_shopping_cart_released);

        imageViewMyInfo = (ImageView) findViewById(R.id.menu_personal_center_image);
//        imageViewMyInfo.setOnTouchListener(myInfoListener);
        imageViewMyInfo.setImageResource(R.drawable.menu_my_info_pressed);
    }

    class LoginListener implements View.OnClickListener {
        private LoginActivity context;
        public LoginListener(LoginActivity context) {
            this.context = context;
        }
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.login:
                    closeKeyBoard();
                    String username = cellPhoneEditText.getText().toString();
                    String password = passwordEditText.getText().toString();
                    Toast toast = null;
                    if (username == null || username.equals("")) {
                        toast = Toast.makeText(context, "用户名不能为空", Toast.LENGTH_SHORT);
                    } else if (password == null || password.equals("")) {
                        toast = Toast.makeText(context, "密码不能为空", Toast.LENGTH_SHORT);
                    }
                    if (toast != null) {
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    } else {
                        Log.d("LoginActivity", "come here");
                        model = new LoginModel();
                        model.setUsername(username);
                        model.setPassword(password);
                        UserInfoController.doLogin(context, model);
                    }
                    break;
                case R.id.register:
                    doActivity(RegisterActivity.class);
                    break;
                case R.id.forget_password:
                    break;
                default:
            }
        }
    }

    @Override
    public void onPreExecute() {
        progressDialog.show();
    }

    @Override
    public void onResponseMessage(BaseResponse response) {
        progressDialog.cancel();
        Log.i("response", Integer.toString(response.getCommandId()));
        if (response.getResult() == 0) {
            // write user information into sharedPreferences
            shared = getSharedPreferences(Constant.PREFERENCE_FILE, Activity.MODE_PRIVATE);
            editor = shared.edit();
            editor.putString("username", model.getUsername());
            editor.putBoolean("login", true);
            editor.putString("password", model.getPassword());
            // success, set result
            Intent intent = new Intent();
            intent.putExtra("username", model.getUsername());
            intent.putExtra("password", model.getPassword());
            intent.putExtra("login", true);
            ((ShoppingApplication)getApplication()).setUsername(model.getUsername());
            setResult(RESULT_OK, intent);
            finish();
        } else {
            Toast toast = Toast.makeText(this, response.getMessage(), Toast.LENGTH_LONG);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    }

    private void closeKeyBoard() {
        InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}
